<?php
/**
 * User MyReservations edit Controller
 *
 * @access 	public
 * @author 	Valentin
 * @copyright	ResMania 2009 all rights reserved.
 * @link		http://developer.resmania.com/api
 */
class RM_User_MyReservations_MainController extends RM_User_Controller
{
    protected $formName = null;

    public function preDispatch()
    {
        parent::preDispatch();
        $user = $this->getUser();
        if ($user == null) {
            $this->_redirect('Unit', 'list');
        }
    }

    /**
     * Returns current loginned resmania user or returns false
     *
     * @return RM_User_Row or null
     */
    protected function getUser()
    {
        $user = null;
        $cmsUser = RM_Environment::getConnector()->getUser();
        if ($cmsUser->isGuest() == false) {
            $user = $cmsUser->findResmaniaUser();            
        }
        return $user;
    }

    /**
     * Returns current reservation by ID
     *
     * @throw RM_Exception
     * @param string $reservationID
     * @return RM_Reservation_Row
     */
    protected function getReservation($reservationID = null)
    {
        if ($reservationID == null) {
            throw new RM_Exception($this->_translate->_(
                'User.MyReservations.Reservation.Main.Errors',
                'ReservationIDNotSpecified'
            ));
        }

        $reservationModel = new RM_Reservations();
        $reservation = $reservationModel->find($reservationID)->current();
        if ($reservation == null) {
            throw new RM_Exception($this->_translate->_(
                'User.MyReservations.Reservation.Main.Errors',
                'WrongReservationID'
            ));
        }

        if ($reservation->isAssignedTo($this->getUser()) == false) {
            throw new RM_Exception($this->_translate->_(
                'User.MyReservations.Reservation.Main.Errors',
                'AccessDenied'
            ));
        }
        return $reservation;
    }


    public function showAction()
    {
        $formModel = new RM_Forms();
        $form = $formModel->find($this->formName)->current();
        $this->view->state = $form->getState();
        $this->view->form = $form;
        $this->view->user = $this->getUser();
    }

    /**
     * This method is really hardcode, we need to rewrite it later
     *
     * @todo rewrite it later
     * @param RM_Reservation_Row $reservation
     * @return
     */
    protected function _getSummaryReservationInformation(RM_Reservation_Row $reservation)
    {
        $config = new RM_Config();
        $currencySymbol = $config->getCurrencySymbol();

        $summaryModel = new RM_ReservationSummary();
        $savedPriceData = array();

        $details = $reservation->findReservationDetails();
        foreach ($details as $detail) {
            $unit = $detail->findUnit();
            if ($unit == null) continue;

            $summaryRows = $summaryModel->fetchByReservationDetail($detail)->toArray();
            for($i = 0; $i < count($summaryRows); $i++){
                $summaryRows[$i]['total_amount'] = $currencySymbol.RM_Environment::getInstance()->roundPrice($summaryRows[$i]['total_amount']);
            }

            $savedPriceData[] ="{
                unit: {
                    id: '".$unit->getId()."',
                    name: '".addslashes($unit->name)."',
                },
                total_sub: '".$currencySymbol.RM_Environment::getInstance()->roundPrice($detail->total_price)."',
                reserved_period: {
                    start_date: '".$detail->getStartDatetime()."',
                    end_date: '".$detail->getEndDatetime()."'
                },
                summary_rows: ".Zend_Json::encode($summaryRows)."
            }";
        }

        $reservationSummaryRows = $summaryModel->fetchByReservation($reservation)->toArray();
        for($i = 0; $i < count($reservationSummaryRows); $i++){
            $reservationSummaryRows[$i]['total_amount'] = $currencySymbol.RM_Environment::getInstance()->roundPrice($reservationSummaryRows[$i]['total_amount']);
        }

        $totalPrice = RM_Environment::getInstance()->roundPrice($reservation->getTotalPrice());
        $totalPaid = RM_Environment::getInstance()->roundPrice($reservation->getTotalPaid());
        $totalDue = RM_Environment::getInstance()->roundPrice($totalPrice - $totalPaid);

        $info = "{
            prices: [".implode(',', $savedPriceData)."],
            reservation_summary_rows: ".Zend_Json::encode($reservationSummaryRows).",
            reservationTotal: '".$currencySymbol.$totalPrice."',
            reservationTotalPaid: '".$currencySymbol.$totalPaid."',
            reservationTotalDue: '".$currencySymbol.$totalDue."',
        }";

        return array($info, $totalDue);
    }
}